import { useLoginStore } from "@/stores/auth";
import router from "@/router/index.js";

export async function userInfoUpdating(isSilent = true) {
  const { checkingUserInfo } = useLoginStore();
  return checkingUserInfo({ isForce: true, isSilent });
}

export async function loginChecking(isSilent) {
  const { checkingLogined } = useLoginStore();
  const isLogined = await checkingLogined();

  if (!isLogined.value && !isSilent) {
    router.push({
      name: "login",
    });
  }

  return isLogined;
}

export async function goByLoginChecking(route, isSilent) {
  let isLogined = await loginChecking(isSilent);

  if (isLogined.value && route) {
    router.push(route);
  }
}

export function doLogout(isRefer) {
  const loginStore = useLoginStore();
  loginStore.clear();
  router.push({
    name: "login",
    query: {
      isEntry: !isRefer,
    },
  });
}

export function clearToken() {
  const loginStore = useLoginStore();
  loginStore.clear();
}

export function useIsLogined() {
  return useLoginStore().getIsLogined();
}
